package JavaImages;

import java.io.*;
import java.util.*;
import javax.imageio.ImageReader;
import javax.imageio.spi.ImageReaderSpi;
import javax.imageio.stream.ImageInputStream;

/**
 *  Simple, non-functional ImageReaderSpi used to understand how 
 *  information regarding format name, suffices and mime types
 *  get passed to ImageIO static methods
 */
public class ch5v1ImageReaderSpi extends ImageReaderSpi {
    
    static final String[] suffixes = { "ch5", "CH5"};
    static final String[] names = {"ch5"};
    static final String[] MIMETypes = { "image/ch5" };    

    static final String version = "0.50";

    static final String readerCN="ch5.imageio.plugins.ch5v1ImageReader"; 

    static final String vendorName = "Company Name";

    //writerSpiNames
    static final String[] wSN={"ch5.imageio.plugins.ch5v1ImageWriterSpi"};

    //StreamMetadataFormatNames and StreamMetadataFormatClassNames
    static final boolean supportedStandardStreamMetadataFormat = false;
    static final String nativeStreamMFN = null;
    static final String nativeStreamMFCN = null;
    static final String[] extraStreamMFN = null;
    static final String[] extraStreamMFCN = null;

    //ImageMetadataFormatNames and ImageMetadataFormatClassNames
    static final boolean supportedStandardImageMetadataFormat = false;
    static final String nativeImageMFN = null;
    static final String nativeImageMFCN = null;
    static final String[] extraImageMFN = {null};
    static final String[] extraImageMFCN = {null};

    public ch5v1ImageReaderSpi() {
	super(vendorName, 
	      version,
	      names,
	      suffixes,
	      MIMETypes,
	      readerCN, // reader class name
	      STANDARD_INPUT_TYPE,
	      wSN, // writer spi names
	      supportedStandardStreamMetadataFormat,
	      nativeStreamMFN,
	      nativeStreamMFCN,
	      extraStreamMFN,
	      extraStreamMFCN,
	      supportedStandardImageMetadataFormat,
	      nativeImageMFN,
	      nativeImageMFCN,
	      extraImageMFN,
	      extraImageMFCN);
    }

    public String getDescription(Locale locale) {
	return "Demo ch5 image reader, version " + version;
    }

    /**
     * We haven't created the corresponding ImageReader class yet,
     * so we'll just return null for now.  
     */
    public ImageReader createReaderInstance(Object extension) {
	return new ch5v1ImageReader(this);
    }

    /**
     * This method gets called when an application wants to see if
     * the input image's format can be decoded by this ImageReader.
     * In this case, we'll simply check the first line of data to 
     * see if it is a 5 which is the format type's magic number.
     * Note that we initially make sure the input object is of
     * type ImageInputStream so we know it is compatible with
     * mark and reset methods.
     */
    public boolean canDecodeInput(Object input) {
	boolean reply = false;

        if (!(input instanceof ImageInputStream)) 
	    return reply;

	ImageInputStream iis = (ImageInputStream)input;
	iis.mark(); // mark where we are in ImageInputStream
	try {
	    String magicNumber = iis.readLine().trim();
	    iis.reset(); // reset stream back to marked location
	    if (magicNumber.equals("5"))
		reply = true;
	}
	catch (IOException exception) {
	}
	return reply;
    }

    /**
     * This method gets called when the set of file suffices is 
     * requested by the ImageIO's getImageReadersBySuffix method
     * It doesn't need to be redefined here, but is done for
     * illustrative purposes
     */
    public String[] getFileSuffixes() {
	return super.getFileSuffixes();
    }

    /**
     * This method gets called when the set of file mime types is
     * requested by the ImageIO's getImageReadersByMIMEType method
     * It doesn't need to be redefined here, but is done for
     * illustrative purposes
     */
    public String[] getMIMETypes() {
	return super.getMIMETypes();
    }

    /**
     * This method gets called when the set of format names is 
     * requested by the ImageIO's getImageReadersByFormatName method
     * It doesn't need to be redefined here, but is done for
     * illustrative purposes
     */
    public String[] getFormatNames() {
	return super.getFormatNames();
    }
}
